package com.runa.monitor.platinterface.extend.third.pojo;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

public class Result<T> {
//    @ApiModelProperty(
//            value = "状态码",
//            name = "resultCode",
//            position = 1
//    )
    private int resultCode;
//    @ApiModelProperty(
//            value = "状态信息",
//            name = "message",
//            position = 2
//    )
    private String message;
//    @ApiModelProperty(
//            value = "数据域",
//            name = "data",
//            position = 3
//    )
    private T data;

    public Result() {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(ResultCode.SUCCESS.message);
    }

    public int getResultCode() {
        return this.resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result genSuccessResult() {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(ResultCode.SUCCESS.message);
        return this;
    }

    public Result genSuccessResult(String message) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(message);
        return this;
    }

    public Result<T> genSuccessResult(T data) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(ResultCode.SUCCESS.message);
        this.setData(data);
        return this;
    }

    public Result<T> genSuccessResult(T data, String message) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(message);
        this.setData(data);
        return this;
    }

    public Result<T> genFailResult(ResultCode resultCode, String message) {
        this.setResultCode(resultCode.code);
        if (StringUtils.isEmpty(message)) {
            this.setMessage(resultCode.message);
        } else {
            this.setMessage(message);
        }

        return this;
    }

    public Result<T> saveSuccessResult(T data) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage("添加成功");
        this.setData(data);
        return this;
    }

    public Result<T> saveFailResult(String reason) {
        this.setResultCode(ResultCode.SYSTEM_ERROR.code);
        String message = "添加失败";
        if (!StringUtils.isEmpty(reason)) {
            message = message + "，" + reason;
        }

        this.setMessage(message);
        return this;
    }

    public Result<T> addFailResult(String reason) {
        this.setResultCode(ResultCode.SYSTEM_ERROR.code);
        String message = "保存失败";
        if (!StringUtils.isEmpty(reason)) {
            message = message + "，" + reason;
        }

        this.setMessage(message);
        return this;
    }

    public Result<T> updateSuccessResult(T data) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage("修改成功");
        this.setData(data);
        return this;
    }

    public Result<T> updateFailResult(String reason) {
        this.setResultCode(ResultCode.SYSTEM_ERROR.code);
        String message = "修改失败";
        if (!StringUtils.isEmpty(reason)) {
            message = message + "，" + reason;
        }

        this.setMessage(message);
        return this;
    }

    public Result<T> deleteSuccessResult(T data) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage("删除成功");
        this.setData(data);
        return this;
    }

    public Result<T> deleteFailResult(String reason) {
        this.setResultCode(ResultCode.SYSTEM_ERROR.code);
        String message = "删除失败";
        if (!StringUtils.isEmpty(reason)) {
            message = message + "，" + reason;
        }

        this.setMessage(message);
        return this;
    }

    public Result<T> customResult(int resultCode, String message, T data) {
        this.resultCode = resultCode;
        this.message = message;
        this.data = data;
        return this;
    }

    public Result<T> handleSuccessResult(String message, T data) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(message);
        this.setData(data);
        return this;
    }

    public Result<T> handleSuccessResult(T data) {
        this.setResultCode(ResultCode.SUCCESS.code);
        this.setMessage(ResultCode.SUCCESS.message);
        this.setData(data);
        return this;
    }

    public Result<T> handleFailResult(String message, T data) {
        this.setResultCode(ResultCode.SYSTEM_ERROR.code);
        this.setMessage(message);
        this.setData(data);
        return this;
    }

    public Result<T> handelResult(int resultCode, String message) {
        this.setResultCode(resultCode);
        this.setMessage(message);
        return this;
    }

    public Result<T> genFailResult(ResultCode resultCode) {
        this.setResultCode(resultCode.code);
        this.setMessage(resultCode.message);
        return this;
    }

//    public static <T> Result<T> ok() {
//        return restResult((Object)null, ResultCode.SUCCESS.code, ResultCode.SUCCESS.message);
//    }
//
//    public static <T> Result<T> ok(T data) {
//        return restResult(data, ResultCode.SUCCESS.code, ResultCode.SUCCESS.message);
//    }
//
//    public static <T> Result<T> ok(T data, String msg) {
//        return restResult(data, ResultCode.SUCCESS.code, msg);
//    }
//
//    public static <T> Result<T> error() {
//        return restResult((Object)null, ResultCode.SYSTEM_ERROR.code, ResultCode.SYSTEM_ERROR.message);
//    }
//
//    public static <T> Result<T> error(String msg) {
//        return restResult((Object)null, ResultCode.SYSTEM_ERROR.code, msg);
//    }
//
//    public static <T> Result<T> error(T data) {
//        return restResult(data, ResultCode.SYSTEM_ERROR.code, ResultCode.SYSTEM_ERROR.message);
//    }
//
//    public static <T> Result<T> error(T data, String msg) {
//        return restResult(data, ResultCode.SYSTEM_ERROR.code, msg);
//    }
//
//    public static <T> Result<T> customize(ResultCode resultCode) {
//        return restResult((Object)null, resultCode.code, resultCode.message);
//    }
//
//    public static <T> Result<T> customize(ResultCode resultCode, String msg) {
//        return restResult((Object)null, resultCode.code, msg);
//    }

    public static <T> Result<T> customize(T data, ResultCode resultCode) {
        return restResult(data, resultCode.code, resultCode.message);
    }

    public static <T> Result<T> customize(T data, ResultCode resultCode, String msg) {
        return restResult(data, resultCode.code, msg);
    }

    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result();
        apiResult.setResultCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }
}

